﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace WindowsStateService
{
    public class CommonPath
    {
        private static string SerialPortDirName = "SerialPort";
        private static string CpuUsageDirName = "CpuUsage";
        private static string CpuTemperatureDirName = "CpuTemperature";
        private static string UsedMemoryRatioDirName = "UsedMemoryRatio";
        private static string DiskUsageDirName = "DiskUsage";
        private static string DiskTemperatureDirName = "DiskTemperature";
        private static string MainBoardTemperatureDirName = "MainBoardTemperature";
        private static string FanSpeedDirName = "FanSpeed";
        private static string ReceivedPackageDirName = "PackageReceived";
        private static string SendPackageDirName = "PackageSend";
        private static string EthernetDirName = "Ethernet";

        public static string SerialPortDirNameDatasFile { get { return GetTadayDatasFile(SerialPortDirName); } }
        public static string CpuUsageDatasFile { get { return GetTadayDatasFile(CpuUsageDirName); } }
        public static string CpuTemperatureDatasFile { get { return GetTadayDatasFile(CpuTemperatureDirName); } }
        public static string UsedMemoryRatioDatasFile { get { return GetTadayDatasFile(UsedMemoryRatioDirName); } }
        public static string DiskUsageDatasFile { get { return GetTadayDatasFile(DiskUsageDirName); } }
        public static string DiskTemperatureDatasFile { get { return GetTadayDatasFile(DiskTemperatureDirName); } }
        public static string MainBoardTemperatureDatasFile { get { return GetTadayDatasFile(MainBoardTemperatureDirName); } }
        public static string FanSpeedDatasFile { get { return GetTadayDatasFile(FanSpeedDirName); } }
        public static string ReceivedPackageDatasFile { get { return GetTadayDatasFile(ReceivedPackageDirName); } }
        public static string SendPackageDatasFile { get { return GetTadayDatasFile(SendPackageDirName); } }
        public static string EthernetDatasFile { get { return GetTadayDatasFile(EthernetDirName); } }

        public static string GetSendPackageDatasFile(string id)
        {
            string file_name = Path.Combine(id, DateTime.Now.ToString("yyyy-MM-dd") + ".data");
            string full_file_name = Path.Combine(MonitorDatasPath, SendPackageDirName, file_name);
            if (!File.Exists(full_file_name))
            {
                FileInfo fileInfo = new FileInfo(full_file_name);
                if (!Directory.Exists(fileInfo.DirectoryName))
                {
                    Directory.CreateDirectory(fileInfo.DirectoryName);
                }
                if (!fileInfo.Exists)
                {
                    var stream = fileInfo.Create();
                    stream.Close();
                }
            }
            return full_file_name;
        }

        public static string GetReceivedPackageDatasFile(string id)
        {
            string file_name = Path.Combine(id, DateTime.Now.ToString("yyyy-MM-dd") + ".data");
            string full_file_name = Path.Combine(MonitorDatasPath, ReceivedPackageDirName, file_name);
            if (!File.Exists(full_file_name))
            {
                FileInfo fileInfo = new FileInfo(full_file_name);
                if (!Directory.Exists(fileInfo.DirectoryName))
                {
                    Directory.CreateDirectory(fileInfo.DirectoryName);
                }
                if (!fileInfo.Exists)
                {
                    var stream = fileInfo.Create();
                    stream.Close();
                }
            }
            return full_file_name;
        }


        public static string MonitorDatasPath
        {
            get
            {
                string specFolder = GetSpecFolder();
                string logDir = specFolder + Path.DirectorySeparatorChar + GetAssemblyVersion();
                string dir = Path.Combine(logDir, "Datas");
                if (!Directory.Exists(dir))
                {
                    Directory.CreateDirectory(dir);
                }
                return dir;
            }
        }

        public static string MonitorConfigFileName 
        {
            get
            {
                string specFolder = GetSpecFolder();
                string logDir = specFolder + Path.DirectorySeparatorChar + GetAssemblyVersion();
                if (!Directory.Exists(logDir))
                {
                    Directory.CreateDirectory(logDir);
                }
                return Path.Combine(logDir, "monitor.config");
            }
        }

        public static string SerialPortConfigFileName
        {
            get
            {
                string specFolder = GetSpecFolder();
                string logDir = specFolder + Path.DirectorySeparatorChar + GetAssemblyVersion();
                if (!Directory.Exists(logDir))
                {
                    Directory.CreateDirectory(logDir);
                }
                return Path.Combine(logDir, "serialport.config");
            }
        }

        public static void CreateMonitorConfigFile()
        {
            if (!File.Exists(MonitorConfigFileName))
            {
                FileInfo fileInfo = new FileInfo(MonitorConfigFileName);
                var stream = fileInfo.Create();
                stream.Dispose();
            }
        }

        public static void CreateSerialPortConfigFile()
        {
            if (!File.Exists(SerialPortConfigFileName))
            {
                FileInfo fileInfo = new FileInfo(SerialPortConfigFileName);
                var stream = fileInfo.Create();
                stream.Dispose();
            }

        }


        public static string ServiceLogFullFileName
        {
            get
            {
                string specFolder = GetSpecFolder();
                string logDir = specFolder + Path.DirectorySeparatorChar + GetAssemblyVersion();
                if (!Directory.Exists(logDir))
                {
                    Directory.CreateDirectory(logDir);
                }
                string file =  Path.Combine(logDir, "service.log");
                if (!File.Exists(file))
                {
                    var stream = File.Create(file);
                    stream.Close();
                }
                return file;
            }
        }

        public static string LogDataPath
        {
            get
            {
                string specFolder = GetSpecFolder();
                //System.Diagnostics.Debugger.Launch();
                string logDir = specFolder + Path.DirectorySeparatorChar + GetAssemblyVersion()
                    + Path.DirectorySeparatorChar + "Logs";
                return logDir;
            }            
        }

        public static string TodayFile
        {
            get
            {
                string path = CommonPath.LogDataPath;
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                string report_file_name = DateTime.Now.ToString("yyyy-MM-dd") + ".log";
                string full_file_name = Path.Combine(path, report_file_name);
                if (!File.Exists(full_file_name))
                {
                    var stream = File.Create(full_file_name);
                    stream.Close();
                }
                return full_file_name;
            }
        }

        public static string GetAssemblyVersion()
        {
            Assembly ass = Assembly.GetEntryAssembly();
            return GetAssemblyVersion(ass);
        }

        private static string GetAssemblyVersion(Assembly ass) 
        {
            //if(ass == null)
            //    ass = Assembly.GetExecutingAssembly();
            //FileVersionInfo info = FileVersionInfo.GetVersionInfo(ass.Location);
            //string version = info.ProductVersion.Trim();
            //return string.IsNullOrWhiteSpace(version) ? "1.0.0.0" : version;
            return "1.0.0.0";
        }


        private static string GetTadayDatasFile(string dir_name)
        {
            string file_name = DateTime.Now.ToString("yyyy-MM-dd") + ".data";
            string full_file_name = Path.Combine(MonitorDatasPath, dir_name, file_name);
            if (!File.Exists(full_file_name))
            {
                FileInfo fileInfo = new FileInfo(full_file_name);
                if (!Directory.Exists(fileInfo.DirectoryName))
                {
                    Directory.CreateDirectory(fileInfo.DirectoryName);
                }
                if (!fileInfo.Exists)
                {
                    var stream = fileInfo.Create();
                    stream.Close();
                }
            }
            return full_file_name;
        }

        private static string GetSpecFolder()
        {
            return ConfigFileHelper.Instance.DataPath;
        }
    }
}
